package com.zzcoder.mall.common.dto;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.servlet.http.HttpServletResponse;

@ApiModel(value = "响应结果实体")
@Data
public class ResponseResult<T> {
    @ApiModelProperty(value = "响应代码")
    private int code; // 200是正常，非200表示异常
    @ApiModelProperty(value = "响应消息")
    private String message;
    @ApiModelProperty(value = "数据")
    private T data;

    public static <T> ResponseResult<T> success() {
        return success(HttpServletResponse.SC_OK, "操作成功", null);
    }

    public static <T> ResponseResult<T> success(T data) {
        return success(HttpServletResponse.SC_OK, "操作成功", data);
    }

    public static <T> ResponseResult<T> success(String message, T data) {
        return success(HttpServletResponse.SC_OK, message, data);
    }

    public static <T> ResponseResult<T> success(int code, String message, T data) {
        ResponseResult r = new ResponseResult();
        r.setCode(code);
        r.setMessage(message);
        r.setData(data);
        return r;
    }

    public static <T> ResponseResult<T> fail(int code) {
        return fail(code, null, null);
    }

    public static <T> ResponseResult<T> fail(String message) {
        return fail(HttpServletResponse.SC_BAD_REQUEST, message, null);
    }

    public static <T> ResponseResult<T> fail(String message, T data) {
        return fail(HttpServletResponse.SC_BAD_REQUEST, message, data);
    }

    public static <T> ResponseResult<T> fail(int code, String message) {
        return fail(code, message, null);
    }

    public static <T> ResponseResult<T> fail(int code, String message, T data) {
        ResponseResult r = new ResponseResult();
        r.setCode(code);
        r.setMessage(message);
        r.setData(data);
        return r;
    }

}
